package com.sandking.io;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.UnpooledByteBufAllocator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bowlong.io.ByteInStream;
import com.bowlong.io.ByteOutStream;
import com.bowlong.objpool.ByteInPool;
import com.bowlong.objpool.ByteOutPool;

public class BsonTest {
	public static void main(String[] args) throws Exception {

		UnpooledByteBufAllocator pool = new UnpooledByteBufAllocator(false);
		for (int j = 0; j < 10; j++) {
			long l1 = System.currentTimeMillis();
			for (int i = 0; i < 100; i++) {
//				ByteOutStream byteOutStream = ByteOutPool.borrowObject();
				// ByteBuf buff = pool.buffer();

				try {
//					s1(ByteOutPool.borrowObject());
					// s2(buff);
					 s3();
				} catch (Exception e) {
					// TODO: handle exception
				} finally {
					// ByteOutPool.returnObject(byteOutStream);
				}
			}
			long l2 = System.currentTimeMillis();
			System.out.println("Tile --------------------->" + (l2 - l1));
		}
	}

	public static void s1(ByteOutStream os) throws Exception {
		BSON.writeByte(os, (byte) 5);
		BSON.writeShort(os, (short) 855);
		BSON.writeInt(os, (int) 85578979);
		BSON.writeLong(os, -129);
		BSON.writeFloat(os, 83197.9456321f);
		BSON.writeDouble(os, 9.1212d);
		BSON.writeDate(os, new Date());
		BSON.writeBoolean(os, false);
		BSON.writeString(os, "abcdefj 我哎中华");

		BSON.writeByteArray(os, new byte[] { 1, 2, 3, 4 });
		BSON.writeShortArray(os, new short[] { 1, 2, 3, 4 });
		BSON.writeIntArray(os, new int[] { 1, 2, 3, 4 });
		BSON.writeLongArray(os, new long[] { 13213132131l, 2321312312l, 3, 4 });
		BSON.writeFloatArray(os, new float[] { 1.1f, 2.1f, 3.0f, 4.0f });
		BSON.writeDoubleArray(os, new double[] { 456451.456d, 7987.772d,
				7879.123d, 4.012d });
		BSON.writeDateArray(os, new Date[] { new Date(), new Date(),
				new Date(), new Date() });
		BSON.writeBooleanArray(os, new boolean[] { false, true, true });
		BSON.writeStringArray(os, new String[] { "a", "b", "c", "d" });

		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put((byte) 1, (byte) 1);
		map.put((short) 1, (short) 1);
		map.put((int) 1, (int) 1);
		map.put((long) 1, (long) 1);
		map.put((float) 1, (float) 1);
		map.put((double) 1, (double) 1);
		map.put((byte) 1, new Date());
		map.put("keykeykey", new boolean[] { false, true, true });
		map.put(false, 32134324535432l);
		BSON.writeMap(os, map);

		List<Object> list = new ArrayList<Object>();
		list.add("1fsaf");
		list.add(new String[] { "a", "b" });
		list.add(new boolean[] { false, true, true });
		list.add(32131.045456d);
		list.add(4444444444l);
		list.add(map);
		BSON.writeList(os, list);
		byte[] data = os.toByteArray();
		ByteInStream in = ByteInPool.borrowObject(data);
		BSON.readByte(in, null);
		BSON.readShort(in, null);
		BSON.readInt(in, null);
		BSON.readLong(in, null);
		BSON.readFloat(in, null);
		BSON.readDouble(in, null);
		BSON.readDate(in, null);
		BSON.readBoolean(in, null);
		BSON.readString(in, null);
		Arrays.toString(BSON.readByteArray(in, null));
		Arrays.toString(BSON.readShortArray(in, null));
		Arrays.toString(BSON.readIntArray(in, null));
		Arrays.toString(BSON.readLongArray(in, null));
		Arrays.toString(BSON.readFloatArray(in, null));
		Arrays.toString(BSON.readDoubleArray(in, null));
		Arrays.toString(BSON.readDateArray(in, null));
		Arrays.toString(BSON.readBooleanArray(in, null));
		Arrays.toString(BSON.readStringArray(in, null));
		BSON.readMap(in, null);
		BSON.readList(in, null);
		ByteInPool.returnObject(in);
	}

	public static void s2(ByteBuf os) throws Exception {
		SK_Buff.writeByte(os, (byte) 5);
		SK_Buff.writeShort(os, (short) 855);
		SK_Buff.writeInt(os, (int) 85578979);
		SK_Buff.writeLong(os, -129);
		SK_Buff.writeFloat(os, 83197.9456321f);
		SK_Buff.writeDouble(os, 9.1212d);
		SK_Buff.writeDate(os, new Date());
		SK_Buff.writeBoolean(os, false);
		SK_Buff.writeString(os, "abcdefj 我哎中华");

		SK_Buff.writeByteArray(os, new byte[] { 1, 2, 3, 4 });
		SK_Buff.writeShortArray(os, new short[] { 1, 2, 3, 4 });
		SK_Buff.writeIntArray(os, new int[] { 1, 2, 3, 4 });
		SK_Buff.writeLongArray(os,
				new long[] { 13213132131l, 2321312312l, 3, 4 });
		SK_Buff.writeFloatArray(os, new float[] { 1.1f, 2.1f, 3.0f, 4.0f });
		SK_Buff.writeDoubleArray(os, new double[] { 456451.456d, 7987.772d,
				7879.123d, 4.012d });
		SK_Buff.writeDateArray(os, new Date[] { new Date(), new Date(),
				new Date(), new Date() });
		SK_Buff.writeBooleanArray(os, new boolean[] { false, true, true });
		SK_Buff.writeStringArray(os, new String[] { "a", "b", "c", "d" });

		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put((byte) 1, (byte) 1);
		map.put((short) 1, (short) 1);
		map.put((int) 1, (int) 1);
		map.put((long) 1, (long) 1);
		map.put((float) 1, (float) 1);
		map.put((double) 1, (double) 1);
		map.put((byte) 1, new Date());
		map.put("keykeykey", new boolean[] { false, true, true });
		map.put(false, 32134324535432l);
		SK_Buff.writeMap(os, map);

		List<Object> list = new ArrayList<Object>();
		list.add("1fsaf");
		list.add(new String[] { "a", "b" });
		list.add(new boolean[] { false, true, true });
		list.add(32131.045456d);
		list.add(4444444444l);
		list.add(map);
		SK_Buff.writeList(os, list);
		SK_Buff.readByte(os, null);
		SK_Buff.readShort(os, null);
		SK_Buff.readInt(os, null);
		SK_Buff.readLong(os, null);
		SK_Buff.readFloat(os, null);
		SK_Buff.readDouble(os, null);
		SK_Buff.readDate(os, null);
		SK_Buff.readBoolean(os, null);
		SK_Buff.readString(os, null);
		Arrays.toString(SK_Buff.readByteArray(os, null));
		Arrays.toString(SK_Buff.readShortArray(os, null));
		Arrays.toString(SK_Buff.readIntArray(os, null));
		Arrays.toString(SK_Buff.readLongArray(os, null));
		Arrays.toString(SK_Buff.readFloatArray(os, null));
		Arrays.toString(SK_Buff.readDoubleArray(os, null));
		Arrays.toString(SK_Buff.readDateArray(os, null));
		Arrays.toString(SK_Buff.readBooleanArray(os, null));
		Arrays.toString(SK_Buff.readStringArray(os, null));
		SK_Buff.readMap(os, null);
		SK_Buff.readList(os, null);
	}

	public static void s3() throws Exception {
		SK_ByteBuff os = SK_ByteBuff.allocate();
		os.writeByte((byte) 5);
		os.writeShort((short) 855);
		os.writeInt((int) 85578979);
		os.writeLong(-129);
		os.writeFloat(83197.9456321f);
		os.writeDouble(9.1212d);
		os.writeDate(new Date());
		os.writeBoolean(false);
		os.writeString("abcdefj 我哎中华");

		os.writeByteArray(new byte[] { 1, 2, 3, 4 });
		os.writeShortArray(new short[] { 1, 2, 3, 4 });
		os.writeIntArray(new int[] { 1, 2, 3, 4 });
		os.writeLongArray(new long[] { 13213132131l, 2321312312l, 3, 4 });
		os.writeFloatArray(new float[] { 1.1f, 2.1f, 3.0f, 4.0f });
		os.writeDoubleArray(new double[] { 456451.456d, 7987.772d, 7879.123d,
				4.012d });
		os.writeDateArray(new Date[] { new Date(), new Date(), new Date(),
				new Date() });
		os.writeBooleanArray(new boolean[] { false, true, true });
		os.writeStringArray(new String[] { "a", "b", "c", "d" });

		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put((byte) 1, (byte) 1);
		map.put((short) 1, (short) 1);
		map.put((int) 1, (int) 1);
		map.put((long) 1, (long) 1);
		map.put((float) 1, (float) 1);
		map.put((double) 1, (double) 1);
		map.put((byte) 1, new Date());
		map.put("keykeykey", new boolean[] { false, true, true });
		map.put(false, 32134324535432l);
		os.writeMap(map);

		List<Object> list = new ArrayList<Object>();
		list.add("1fsaf");
		list.add(new String[] { "a", "b" });
		list.add(new boolean[] { false, true, true });
		list.add(32131.045456d);
		list.add(4444444444l);
		list.add(map);
		os.writeList(list);
		
		os = SK_ByteBuff.wrap(os.toByte());
		os.readByte(null);
		os.readShort(null);
		os.readInt(null);
		os.readLong(null);
		os.readFloat(null);
		os.readDouble(null);
		os.readDate(null);
		os.readBoolean(null);
		os.readString(null);
		Arrays.toString(os.readByteArray(null));
		Arrays.toString(os.readShortArray(null));
		Arrays.toString(os.readIntArray(null));
		Arrays.toString(os.readLongArray(null));
		Arrays.toString(os.readFloatArray(null));
		Arrays.toString(os.readDoubleArray(null));
		Arrays.toString(os.readDateArray(null));
		Arrays.toString(os.readBooleanArray(null));
		Arrays.toString(os.readStringArray(null));
		os.readMap(null);
		os.readList(null);
	}
}
